package com.keson.blue;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;

//        原文链接：https://blog.csdn.net/wanderer_ankey/article/details/78488834
//        权限<uses-permission android:name="android.permission.BLUETOOTH" />
//            <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
public class BluetoothUtil {
    private static BluetoothAdapter bluetoothAdapter;

    static {
        // 获得蓝牙适配器对象
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    }

    public static boolean getBlueToothState() {
        // 获取蓝牙状态
        return bluetoothAdapter.isEnabled();
    }

    public static boolean openBlueTooth() {
        if (getBlueToothState()) return true;
        // 打开蓝牙
        return bluetoothAdapter.enable();
    }

    public static boolean closeBlueTooth() {
        if (!getBlueToothState()) return true;
        // 关闭蓝牙
        return bluetoothAdapter.disable();
    }

    // 调用系统的请求打开蓝牙
    public static void gotoSystem(Context context){
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        context.startActivity(intent);
    }

}